//
//  TrainingRecordsViewModel.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/7.
//

import Foundation
import RxCocoa
import MYModule

open class TrainingRecordsViewModel: NSObject {

    public var sections: [TrainingRecordsSectionModel] = []

    public override init() {
        super.init()
        getData()
    }

    private func getData() {
        
        // 查询所有
        let allModels = [TrainingModel].descAll()
        // 每一天的放一个数组
       let allSectionModels = allModels.map { arr ->TrainingRecordsSectionModel? in
           
           
           if arr.isEmpty {
               return nil
           }
           
            var title = ""
            if let fitst = arr.first {
            title = "\(fitst.date.localString)"
            }
           
           let section = TrainingRecordsSectionModel()
           section.title = title
           section.items = arr.filter {
               // 删除不支持的运动
               guard let _ = TrainingType(rawValue: $0.type) else {
                   return false
               }
               guard $0.endTime - $0.startTime >= 60 else {
                   return false
               }
               return true
           }
           
           if section.items.isEmpty {
               return nil
           }
           
           return section
        
        }
        
        sections = allSectionModels.compactMap { $0 }
        
    }
}
